// 'use strict';

console.log('第一题');

var arr = [1,2,3,4];
var item = 3;
function indexOf(arr,item){
    for(i=0;i<arr.length;i++){
        if(arr[i]==item)return i;
    }
    return -i;
}
console.log(arr.indexOf(item));

console.log('第二题');

var arr =[ 1, 2, 3, 4 ]

function sum(arr) {
    var sum = 0;
    for(var i = 0; i < arr.length; i++){
        sum += arr[i];
    }
    return sum;
}
console.log(sum(arr));

console.log('第三题');

var arr1 = [1,2,3,4];
var item1 = 2;
function remove(arr1,item1){
    var a = arr1.slice(0);
    for(var i = 0;i<arr1.length;i++){
        if(a[i]==item1){
            a.splice(i,1);
            i--;
        }
    }
    return a;
}
console.log(remove(arr1,item));

console.log('第四题');

var arr = [1, 2, 2, 3, 4, 2, 2];
var item = 2;
function dontremove(arr,item){
    for(var i = 0;i<arr.length;i++){
        if(arr[i]==item){
            arr.splice(i,1);
            i--;
        }
    }
    return arr;
}
console.log(dontremove(arr,item));

console.log('第五题');

var arr = [1,2,3,4];
var item = 10;
function Add(arr,item){
    var arr1 = arr.splice(0);
    arr1.push(item);
    return arr1;
}
console.log(Add(arr,item));

console.log('第六题');

var arr = [1,2,3,4]
function Delete(arr){
    var arr1 = [];
    var x = arr.length-1;
    while(x--){
        arr1.unshift(arr[x]);
    }
    return arr1;
}
console.log(Delete(arr));

console.log('第七题');

let arr6 = [1,2,3,4];

let item4 = 10;

function prepend(arr, item) {
    let newarr = [];
    for(let i =0;i<arr.length;i++){
        newarr.push(arr[i]);
    }
    newarr.splice(0,0,item);
    return newarr;
}
console.log(prepend(arr6,item4));


console.log('第八题');

var arr = [1,2,3,4];
var item = 1;
function HeaderAdd(arr,item){
    var arr1 = arr.slice(0);
    arr1.shift(item);
    return arr1;
}
console.log(HeaderAdd(arr,item));



console.log('第九题');

var arr1=[1,3,3,7,9];
var arr2=["a","b","c"];
function as(arr1,arr2)
{
    var b =arr1.slice(0);
    var c =arr2.slice(0);
    return b.concat(c);
}
console.log(as(arr1,arr2));

console.log('第十题');

function insert(arr, item, index) {
    var newArr = arr.slice();
    newArr.splice(index, 0, item);
    return newArr;
}
var result10 = insert([1, 2, 3, 4], 'z', 2);
console.log(result10);

console.log('第十一题');

var arr =[1, 2, 4, 4, 3, 4, 3]

var item=4 ;

var count =0;
//循环
for(var i=0;i<arr.length;i++){
if(arr[i]==item){
    count++;
}
}
console.log(count);

console.log('第十二题');

var arr =[1, 2, 4, 4, 3, 3, 1, 5, 3];

function Like(arr) {
    //新数组
    var a=[]; 
    //for循环
    for(var i=0;i<arr.length-1;i++) { 

    for(var j=i+1;j<arr.length;j++) { 
        //如果有重复的元素并且a数组该数的下表为-1
        if(arr[i]===arr[j]&&a.indexOf(arr[i])===-1)  { 
            //数组的末尾添加元素
            a.push(arr[i]); 
        } 
 } 
} 
//将该数组的元素排序
  return a.sort();
}
//打印
console.log(Like(arr))


console.log('第十三题');

var arr =[1, 2, 3, 4];

  function squarel(arr) {
      //新数组
       var arr1 = arr.slice(0);
       //遍历
       for (var i = 0;i<arr1.length; i++){

           arr1[i]=arr[i]*arr[i];
       }
       return arr1;
    }  
    //打印结果为[4, 16, 36, 64, 100, 144, 196, 256, 324, 400]
    console.log(squarel(arr));


console.log('第十四题');

function findAllOccurrences(arr, item) {
    var eqIndex = [];
    arr.forEach(function (value, index) {
        if (item === value) {
            eqIndex.push(index);
        }
    });
    return eqIndex;
}
var result14 = findAllOccurrences(['a', 'b', 'c', 'd', 'e', 'f', 'a', 'b', 'c'], 'a');
console.log(result14);

console.log('第十五题');

function identity(val1, val2) {
    if (val1 === val2) {
        return true;
    } else {
        return false;
    }
}
var result15 = identity(6, '6');
console.log(result15);

console.log('第十六题');

function area_of_circle(r, pi) {
    var p = pi || 3.14
    return p * r * r;
}
var result16 = area_of_circle(2);
console.log(result16);

console.log('第十七题');

// // 1. 原生JS：
// var list = document.getElementById('test-list');
// var arr = ['JavaScript', 'HTML', 'CSS'];
// for (var i of list.children) {
//     if (arr.indexOf(i.innerText) === -1) {
//         list.removeChild(i);
//     }
// }

// 2. jQuery：
var lis = $('#test-list li');
var arr2 = ['JavaScript', 'HTML', 'CSS'];
for (var i of lis) {
    if (arr2.indexOf($(i).text()) === -1) {
        i.remove();
    }
}

console.log('第十八题');

var 
    form = $('#test-form'),

    langs = form.find('[name=lang]'),

    selectAll = form.find('label.selectAll :checkbox'),

    selectAllLabel = form.find('span.selectAll'),

    deselectAllLabel = form.find('span.deselectAll'),

    invertSelect = form.find('a.invertSelect');

console.log(langs);
console.log(selectAll);
console.log(selectAllLabel);
console.log(deselectAllLabel);
console.log(invertSelect);

langs.click(function(){
    
    if(langs.not(':checked').length===0){
       
        selectAll.prop('checked',true);
        
        selectAllLabel.css('display','none');

        deselectAllLabel.css('display','inline');
    }
    else{
        selectAll.prop('checked',false);
        
        selectAllLabel.css('display','inline');

        deselectAllLabel.css('display','none');
    }
});

selectAll.click(function(){
    
    if(selectAll.is(':checked')){
    
        langs.prop('checked',true);

        selectAllLabel.css('display','none');

        deselectAllLabel.css('display','inline');
    }
    else{
        langs.prop('checked',false);
        
        selectAllLabel.css('display','inline');

        deselectAllLabel.css('display','none');
    }
});

invertSelect.click(function(){
   
    langs.map(function(){
   
        let ch = $(this);
   
        if(ch.prop('checked')){
     
            ch.prop('checked',false);
        }
        else{
            ch.prop('checked',true);
        }
    });
    if(langs.not(':checked').length===langs.length){

        selectAll.prop('checked',false);
    
    }else if(langs.not(':checked').length===0){
    
        selectAll.prop('checked',true);
    
    }
});
